package lcmpt;

import java.util.List;

import org.marc4j.marc.DataField;
import org.marc4j.marc.Record;
import org.marc4j.marc.Subfield;
import org.marc4j.marc.VariableField;

import de.dnb.basics.filtering.FilterUtils;
import de.dnb.basics.filtering.IFunction;

public class MarcUtils {

	public static String getPreferredName(Record record) {
		DataField dataField = (DataField) record.getVariableField("162");
		Subfield subfield = dataField.getSubfield('a');
		return subfield.getData();
	}

	public static String getName(DataField dataField) {
		Subfield subfield = dataField.getSubfield('a');
		return subfield.getData();
	}

	public static List<String> getOBB(Record record) {
		List<VariableField> variableFields = record.getVariableFields("562");
		IFunction<VariableField, String> function = new IFunction<VariableField, String>() {
			@Override
			public String apply(VariableField x) {
				return getName((DataField) x);
			}
		};
		return FilterUtils.map(variableFields, function);
	}

	public static boolean hasOB(Record record) {
		return !getOBB(record).isEmpty();
	}

	public static final String htmlOpen = "\n<p style=\"text-align:left; "
			+ "margin-left:50px; margin-right:50px\">";
	public static final String htmlClose = "</p>";

	public static String getSourcesAsHTML(Record record) {
		List<VariableField> variableFields = record.getVariableFields("670");
		if (variableFields.isEmpty())
			return "";
		String sources = "<h2>Quellen:</h2>";
		for (VariableField variableField : variableFields) {
			sources += htmlOpen;
			DataField dataField = (DataField) variableField;
			Subfield source = dataField.getSubfield('a');
			if (source != null)
				sources += "<b>in:</b> " + source.getData();
			Subfield def = dataField.getSubfield('b');
			if (def != null) {
				String defS = def.getData();
				sources += "<br>" + "<b>als:</b> " + defS;
			}
			sources += htmlClose;
		}
		return sources;
	}

	public static String getSynonymsAsHTML(Record record) {
		List<VariableField> variableFields = record.getVariableFields("462");
		if (variableFields.isEmpty())
			return "";
		String syns = "<h2>Synonyme:</h2>";
		for (VariableField variableField : variableFields) {
			syns += htmlOpen;
			DataField dataField = (DataField) variableField;
			Subfield syn = dataField.getSubfield('a');
			syns += syn.getData();
			syns += htmlClose;
		}
		return syns;
	}

	public static String getOBBAsHTML(Record record) {
		List<String> obb = getOBB(record);
		if (obb.isEmpty())
			return "";
		String syns = "<h2>Oberbegriffe:</h2>";
		for (String ob : obb) {
			syns += htmlOpen;
			syns += ob;
			syns += htmlClose;
		}
		return syns;
	}

	public static String getExplanatoriesAsHTML(Record record) {
		List<VariableField> variableFields = record.getVariableFields("680");
		if (variableFields.isEmpty())
			return "";
		String syns = "<h2>Erläuterungen:</h2>";
		for (VariableField variableField : variableFields) {
			syns += htmlOpen;
			DataField dataField = (DataField) variableField;
			List<Subfield> synList = dataField.getSubfields();
			for (Subfield subfield : synList) {
				syns += subfield.getData() + " ";
			}
			syns += htmlClose;
		}
		return syns;
	}

	public static String getTracingNotesAsHTML(Record record) {
		List<VariableField> variableFields = record.getVariableFields("681");
		if (variableFields.isEmpty())
			return "";
		String syns = "<h2>Verweisungsvermerke:</h2>";
		for (VariableField variableField : variableFields) {
			syns += htmlOpen;
			DataField dataField = (DataField) variableField;
			List<Subfield> synList = dataField.getSubfields();
			for (Subfield subfield : synList) {
				syns += subfield.getData() + " ";
			}
			syns += htmlClose;
		}
		return syns;
	}

	public static String getRecordAsHTML(Record record) {
		String r = record.toString();
		r = r.replace("\n", "<br>");
		return "<h3>Marc-Datensatz:</h3>" + htmlOpen + r + htmlClose;
	}

}
